package com.xh.clean.modular.clean.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.xh.clean.core.base.controller.BaseController;
import com.xh.clean.core.common.constant.Const;
import com.xh.clean.core.log.LogObjectHolder;
import com.xh.clean.core.shiro.ShiroKit;
import com.xh.clean.modular.clean.model.PatrolArea;
import com.xh.clean.modular.clean.service.IPatrolAreaService;
import com.xh.clean.modular.clean.service.IStoreyService;
import com.xh.clean.modular.clean.warpper.PatrolAreaWarpper;
import com.xh.clean.modular.system.service.IDictService;
import com.xh.clean.modular.system.transfer.BootstrapSuggestVo;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 巡视区域表控制器
 *
 * @author yangjie
 * @Date 2018-07-05 17:53:11
 */
@Controller
@RequestMapping("/patrolArea")
public class PatrolAreaController extends BaseController {

    private String PREFIX = "/clean/patrolArea/";

    @Autowired
    private IPatrolAreaService patrolAreaService;
    @Autowired
    private IStoreyService storeyService;
    @Autowired
    private IDictService dictService;
    /**
     * 跳转到巡视区域表首页
     */
    @RequestMapping("")
    public String index(Model model) {
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        return PREFIX + "patrolArea.html";
    }

    /**
     * 跳转到添加巡视区域表
     */
    @RequestMapping("/patrolArea_add")
    public String patrolAreaAdd(Model model) {
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        List<Map<String, Object>> patrolAreas = dictService.selectByPid(Const.PATROL_AREA_PID);
        model.addAttribute("patrolAreas", patrolAreas);
        return PREFIX + "patrolArea_add.html";
    }

    /**
     * 跳转到修改巡视区域表
     */
    @RequestMapping("/patrolArea_update/{patrolAreaId}")
    public String patrolAreaUpdate(@PathVariable Integer patrolAreaId, Model model) {
        PatrolArea patrolArea = patrolAreaService.selectById(patrolAreaId);
        model.addAttribute("item",patrolArea);
        List<Map<String, Object>> list = storeyService.selectStoreys("");
        model.addAttribute("list", list);
        List<Map<String, Object>> patrolAreas = dictService.selectByPid(Const.PATROL_AREA_PID);
        model.addAttribute("patrolAreas", patrolAreas);
        LogObjectHolder.me().set(patrolArea);
        return PREFIX + "patrolArea_edit.html";
    }

    /**
     * 获取巡视区域表列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false)Integer storeyId, @RequestParam(required = false) String areaName, @RequestParam(required = false) String position, @RequestParam(required = false) Integer type) {
        Wrapper<PatrolArea> wrapper = new EntityWrapper<PatrolArea>();
        if (StringUtils.isNotEmpty(areaName)) {
            wrapper.like("name", areaName);
        }
        if (StringUtils.isNotEmpty(position)) {
            wrapper.eq("position", position);
        }
        if (type != null) {
            wrapper.eq("type", type);
        }
        if (storeyId != null) {
            wrapper.eq("storey_id", storeyId);
        }
        List<Map<String, Object>> patrolAreas = patrolAreaService.selectMaps(wrapper);
        return super.warpObject(new PatrolAreaWarpper(patrolAreas));
    }
    /**
     * 获取巡视区域表列表
     */
    @RequestMapping(value = "/searchPatrolAreaList")
    @ResponseBody
    public BootstrapSuggestVo searchPatrolAreaList(@RequestParam(required = false)Integer storeyId, @RequestParam(required = false) String areaName, @RequestParam(required = false) String position, @RequestParam(required = false) Integer type) {
        Wrapper<PatrolArea> wrapper = new EntityWrapper<PatrolArea>();
        if (StringUtils.isNotEmpty(areaName)) {
            wrapper.like("name", areaName);
        }
        if (StringUtils.isNotEmpty(position)) {
            wrapper.eq("position", position);
        }
        if (type != null) {
            wrapper.eq("type", type);
        }
        if (storeyId != null) {
            wrapper.eq("storey_id", storeyId);
        }
        BootstrapSuggestVo bootstrapSuggestVo = new BootstrapSuggestVo();
        List<PatrolArea> patrolAreas = patrolAreaService.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(patrolAreas)) {
            bootstrapSuggestVo.setValue(patrolAreas);
        }
        return bootstrapSuggestVo;
    }
    /**
     * 新增巡视区域表
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(PatrolArea patrolArea) {
        patrolArea.setCreatetime(new Date());
        patrolArea.setCreater(ShiroKit.getUser().getId());
        patrolAreaService.insert(patrolArea);
        return SUCCESS_TIP;
    }

    /**
     * 删除巡视区域表
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer patrolAreaId) {
        patrolAreaService.deleteById(patrolAreaId);
        return SUCCESS_TIP;
    }

    /**
     * 修改巡视区域表
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(PatrolArea patrolArea) {
        patrolAreaService.updateById(patrolArea);
        return SUCCESS_TIP;
    }

    /**
     * 巡视区域表详情
     */
    @RequestMapping(value = "/detail/{patrolAreaId}")
    @ResponseBody
    public Object detail(@PathVariable("patrolAreaId") Integer patrolAreaId) {
        return patrolAreaService.selectById(patrolAreaId);
    }
}
